package com.cqc.manage.controller.admin;


import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.cqc.manage.dto.JsonResultDto;
import com.cqc.manage.dto.PayloadDto;
import com.cqc.manage.entity.User;
import com.cqc.manage.service.Imp.user.UserService;
import com.cqc.manage.token.JwtTokenService;
import com.nimbusds.jose.JOSEException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户
 */
@RestController
@RequestMapping("/users")
@Api(tags = "user", description = "用户相关")
public class UserController {

    private static Logger LOG = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private JwtTokenService jwtTokenService;

    /**
     * 用户登录
     *
     * @return
     */
    @ApiOperation(value = "用户登录", notes = "用户登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON userLogin(@RequestParam("userAccount")String userAccount,@RequestParam("userPassword")String userPassword) {
        User byUserAccountAndPassword = userService.findByUserAccountAndPassword(userAccount, userPassword);
        LOG.info("用户登录结果,user:{}", byUserAccountAndPassword);
        try {
            PayloadDto payloadDto = jwtTokenService.getDefaultPayloadDto(byUserAccountAndPassword);
            String token = jwtTokenService.generateTokenByHMAC(JSONUtil.toJsonStr(payloadDto));
            return JSONUtil.parse(new JsonResultDto<String>("登录成功", "200", "Bearer:" + token));
        } catch (JOSEException e) {
            e.printStackTrace();
        }
        return JSONUtil.parse(new JsonResultDto("201", "登录失败"));
    }

    @ApiOperation(value = "添加用户", notes = "添加用户")
    @RequestMapping(value = "/adduser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON addUser(@RequestBody User user) {
        LOG.info("添加用户请求:{}", user);
        User user1 = userService.addUser(user);
        LOG.info("添加用户的结果:{}", user1);
        return JSONUtil.parse(new JsonResultDto<>("200", "success"));
    }

    /**
     * 更新用户必须要传入id
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "更新用户", notes = "更新用户必须要传入用户id")
    @RequestMapping(value = "/updateuser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON updateUser(@RequestBody User user) {
        LOG.info("更新用户请求:{}", user);
        User user1 = userService.updateUser(user);
        LOG.info("更新用户的结果:{}", user1);
        if (user1 == null) {
            return JSONUtil.parse(new JsonResultDto<>("201", "修改用户失败"));
        }
        return JSONUtil.parse(new JsonResultDto<>("200", "修改用户成功"));
    }

    /**
     * 分页查询
     *
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "用户分页查询")
    @RequestMapping(value = "/query", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON query(@RequestParam(value = "pagenum",defaultValue = "0") int pageNum, @RequestParam(value = "pagesize",defaultValue = "10") int pageSize) {
        Pageable pageable = PageRequest.of(pageNum-1, pageSize);
        Page<User> query = userService.query(pageable);
        return JSONUtil.parse(new JsonResultDto<Page<User>>("查询成功", "200",query,query.getTotalElements()));
    }


    @ApiOperation(value = "通过userID查询用户")
    @RequestMapping(value="/queryByUerId",method=RequestMethod.GET)
    public JSON queryByUerId(@RequestParam("ID") String id){
        User user = userService.queryByUserId(id);
        return JSONUtil.parse(new JsonResultDto<User>("success","200",user));
    }

    @ApiOperation(value = "改变用户状态")
    @RequestMapping(value = "/changeUserStatus",method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON changeUserStatus(@RequestBody User user){
        LOG.info("用户:{}",user);
        User user1 = userService.setUserStatus(user);
        return JSONUtil.parse(new JsonResultDto<>("200","success"));
    }

    @ApiOperation(value = "通过userID删除用户")
    @RequestMapping(value="/deleteUser",method=RequestMethod.DELETE)
    public JSON deleteByUerId(@RequestParam("userID") String id){
        userService.delteUser(id);
        return JSONUtil.parse(new JsonResultDto<User>("200","success"));
    }

    @ApiOperation(value = "获取用户名")
    @RequestMapping(value="/getUserName",method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON deleteByUerId(){
        List<Map<String, String>> allUserName = userService.getAllUserName();
        return JSONUtil.parse(new JsonResultDto<List<Map<String, String>>>("success","200",allUserName));
    }


}
